package com.healthteches;

import de.beckhoff.jni.Convert;
import de.beckhoff.jni.JNIByteBuffer;
import de.beckhoff.jni.tcads.AdsCallDllFunction;
import de.beckhoff.jni.tcads.AmsAddr;

/**
 * ClassName : com.tk.iot.beckhoff.ServerCore Description : Auth : engDu Version: 1.0 Date
 * :2019/12/5 16:51
 */
public class ServerCore {

  public static void readInt() {

    long nPort, nErr; // 169.254.21.251.1.1
    AmsAddr addr = new AmsAddr();
    System.out.println("addr:" + addr);
    JNIByteBuffer hdlBuff = new JNIByteBuffer(4);
    // Symbol name: String to byte[]
    JNIByteBuffer symBuff = new JNIByteBuffer(Convert.StringToByteArr("MAIN.PLCVar"));
    JNIByteBuffer dataBuff = new JNIByteBuffer(4);

    // Open communication
    nPort = AdsCallDllFunction.adsPortOpen();
    nErr = AdsCallDllFunction.getLocalAddress(addr);
    addr.setNetIdString("169.254.21.251.1.1"); // 这里填写远程控制器的AMDNETID地址，本地控制器不需要这行指令
    addr.setPort(801); // PLC的端口号

    if (nErr != 0) {
      System.out.println("Error: Open Communication: 0x" + Long.toHexString(nErr));
    }

    // Get handle by symbol name
    nErr =
        AdsCallDllFunction.adsSyncReadWriteReq(
            addr,
            0xF003,
            0x0,
            hdlBuff.getUsedBytesCount(),
            hdlBuff,
            symBuff.getUsedBytesCount(),
            symBuff);
    if (nErr != 0) {
      System.out.println("Error: Get handle: 0x" + Long.toHexString(nErr));
    }

    // Handle: byte[] to int
    int hdlBuffToInt = Convert.ByteArrToInt(hdlBuff.getByteArray());
    // Read value by handle
    nErr = AdsCallDllFunction.adsSyncReadReq(addr, 0xF005, hdlBuffToInt, 0x4, dataBuff);
    if (nErr != 0) {
      System.out.println("Error: Read by handle: 0x" + Long.toHexString(nErr));
    } else {
      // Data: byte[] to int
      int intVal = Convert.ByteArrToInt(dataBuff.getByteArray());
      System.out.println("PLCVar value: " + intVal);
    }

    // Close communication
    nErr = AdsCallDllFunction.adsPortClose();
    if (nErr != 0) {
      System.out.println("Error: Close Communication: 0x" + Long.toHexString(nErr));
    }

    try {
      System.in.read();
    } catch (Exception e) {
      System.out.println("Error: Close program");
    }
  }

  public static void main(String[] args) {
    readInt();
    writeInt();
  }

  public static void writeInt() {
    long nPort, nErr;
    AmsAddr addr = new AmsAddr();
    JNIByteBuffer hdlBuff = new JNIByteBuffer(4);
    // Symbol name: String to byte[]
    JNIByteBuffer symBuff = new JNIByteBuffer(Convert.StringToByteArr("MAIN.PLCWriteVar"));
    // JNIByteBuffer dataBuff = new JNIByteBuffer(4);

    byte[] dataVal = new byte[] {52};
    //        Scanner scan=new Scanner(System.in);
    //        String str = scan.nextLine();
    //        byte []dataVal=str.getBytes();

    // Open communication
    nPort = AdsCallDllFunction.adsPortOpen();
    nErr = AdsCallDllFunction.getLocalAddress(addr);

    addr.setNetIdString("169.254.21.251.1.1"); // 这里填写远程控制器的AMDNETID地址，本地控制器不需要这行指令
    addr.setPort(801); // PLC的端口号

    if (nErr != 0) {
      System.out.println("Error: Open Communication: 0x" + Long.toHexString(nErr));
    }

    // Get handle by symbol name
    nErr =
        AdsCallDllFunction.adsSyncReadWriteReq(
            addr,
            0xF003,
            0x0,
            hdlBuff.getUsedBytesCount(),
            hdlBuff,
            symBuff.getUsedBytesCount(),
            symBuff);
    if (nErr != 0) {
      System.out.println("Error: Get handle: 0x" + Long.toHexString(nErr));
    }

    // Handle: byte[] to int
    int hdlBuffToInt = Convert.ByteArrToInt(hdlBuff.getByteArray());
    // Write value by handle
    nErr = AdsCallDllFunction.adsSyncWriteReqArray(addr, 0xF005, hdlBuffToInt, 0x1, dataVal);

    // Close communication
    nErr = AdsCallDllFunction.adsPortClose();
    if (nErr != 0) {
      System.out.println("Error: Close Communication: 0x" + Long.toHexString(nErr));
    }

    //        try{
    //
    //        }
    //        catch (Exception e){
    //            System.out.println("Error: Close program");
    //        }
  }
}
